package com.snap.vseries.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.snap.vseries.MoleApplication;
import com.snap.vseries.R;
import com.snap.vseries.bean.ProgramSection;
import com.snap.vseries.bean.ProgramStage;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class StageView extends LinearLayout {
    private static final int section_width = AutoSizeUtils.dp2px(MoleApplication.get(), 76);
    /**
     * 中部所占比重
     */
    private final float weightContainer = 5f;
    /**
     * 底部所占比重
     */
    private final float weightFooter = 1f;
    private final Path path;
    private List<SectionView> sectionViews = new ArrayList<>();
    private LinearLayout sectionContainer;
    private ProgramStage programStage;
    private Paint paint;
    private int textWidth = AutoSizeUtils.dp2px(getContext(), 40);

    public StageView(Context context) {
        super(context);
        path = new Path();
    }

    public StageView(Context context, ProgramStage programStage, FragmentManager fm) {
        super(context);
        this.programStage = programStage;
        setOrientation(LinearLayout.VERTICAL);
        setWillNotDraw(false);
        //初始化对象
        paint = new Paint();
        paint.setAntiAlias(true);
        path = new Path();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(ContextCompat.getColor(getContext(), R.color.gray8));
        paint.setStrokeWidth(1);
        resize();
        addContainer();
        addFooter();
    }

    /**
     * 根据节的数量重设宽度
     */
    private void resize() {
        LayoutParams lp = (LayoutParams) getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(-2, -2);
        }
        lp.width = section_width * programStage.getSections().size();
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        setLayoutParams(lp);
    }


    /**
     * 创建段的中间主框架
     */
    private void addContainer() {
        sectionContainer = new LinearLayout(getContext());
        sectionContainer.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
        lp.weight = weightContainer;
        sectionContainer.setLayoutParams(lp);
        addView(sectionContainer);
        for (int i = 0; i < programStage.getSections().size(); i++) {
            addSectionView(programStage.getSections().get(i), false);
        }
    }

    /**
     * 创建段的底部
     */
    private void addFooter() {
        LayoutParams params = new LayoutParams(textWidth, AutoSizeUtils.dp2px(getContext(), 16));
        TextButton footTv = new TextButton(getContext());
        footTv.setId(R.id.stage_cycle_tv);
        footTv.setGravity(Gravity.CENTER);
        footTv.setTextColor(ContextCompat.getColor(getContext(), R.color.gray8));
        if (programStage.getCycleCount() != 1) {
            footTv.setText(String.format(Locale.CHINA, "%dx", programStage.getCycleCount()));
        }
        footTv.setTextSize(12);
        footTv.setLayoutParams(params);

        LinearLayout layout = new LinearLayout(getContext());
        layout.setGravity(Gravity.CENTER);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 0);
        lp.weight = weightFooter;
        footTv.bringToFront();
        layout.addView(footTv);
        addView(layout, lp);
    }

    /**
     * 在此段的最后添加节
     */
    public void addSectionView(ProgramSection section, boolean resize) {
        addSectionView(section, sectionContainer.getChildCount(), resize);
    }

    /**
     * 在此段中的第index出添加节
     */
    public void addSectionView(ProgramSection section, int index, boolean resize) {
        SectionView sectionView = new SectionView(getContext(), section);
        LayoutParams lp = new LayoutParams(section_width, ViewGroup.LayoutParams.MATCH_PARENT);
        sectionContainer.addView(sectionView, index, lp);
        sectionViews.add(index, sectionView);
        if (resize) {
            resize();
        }
    }

    public List<SectionView> getAllSectionView() {
        return sectionViews;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        //绘制Footer旁边的线条|_____|
        if (programStage.getCycleCount() != 1) {
            float weightTotal = weightFooter + weightContainer;
            float heightItem = getHeight() / weightTotal;
            //底部容器的高度
            float height = heightItem / 2f;
            float width = getWidth();
            float distanceToSide = AutoSizeUtils.dp2px(getContext(), 2);
            //竖线的高度
            float lineHeight = AutoSizeUtils.dp2px(getContext(), 8);
            //底部容器的垂直方向上一半的Y坐标
            float footCenterY = getHeight() - height;
            path.moveTo(distanceToSide, footCenterY - lineHeight);
            path.lineTo(distanceToSide, footCenterY);
            path.lineTo((width - textWidth) / 2f, footCenterY);
            canvas.drawPath(path, paint);
            path.reset();
            path.moveTo((width + textWidth) / 2f, footCenterY);
            path.lineTo(getWidth() - distanceToSide, footCenterY);
            path.lineTo(getWidth() - distanceToSide, footCenterY - lineHeight);
            canvas.drawPath(path, paint);
        }
    }
}